<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" class="x-admin-sm">

<head>
    <meta charset="UTF-8">
    <title>日志列表-blog</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="stylesheet" th:href="@{/X-admin/css/font.css}">
    <link rel="stylesheet" th:href="@{/X-admin/css/xadmin.css}">
    <link rel="stylesheet" th:href="@{/X-admin/css/formSelects-v4.css}">
    <!-- jQuery -->
    <script type="text/javascript" th:src="@{/X-admin/js/jquery.min.js}"></script>
    <script type="text/javascript" th:src="@{/X-admin/lib/layui/layui.js}" charset="utf-8"></script>
    <script type="text/javascript" th:src="@{/X-admin/js/xadmin.js}" charset="utf-8"></script>
    <script type="text/javascript" th:src="@{/X-admin/js/formSelects-v4.min.js}" charset="utf-8"></script>
    <script th:src="@{/js/js_utils/checkCron.js}"></script>

    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->

    <style>
        .cronCon {
            margin: 10px;
        }

        .cronCon p:first-child {
            font-size: 16px;
        }

        .cronCon p {
            font-size: 15px;
            margin: 5px 0;
        }

        #layerDemo {
            background: #eee;
        }
    </style>
</head>

<body>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body ">
                    <form class="layui-form layui-col-space5">

                        <div class="layui-col-md2 layui-show-xs-block">
                            <select class="layui-select" name="methodType" xm-select="methodType" xm-select-radio="">
                                <option value="">请选择操作类型</option>
                            </select>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">日志记录时间</label>
                            <div class="layui-input-inline">
                                <input type="text" name="gmtCreate" id="date" lay-verify="date" placeholder="yyyy-MM-dd"
                                       autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline layui-show-xs-block">
                            <button class="layui-btn" lay-submit="" lay-filter="search_btn"><i class="layui-icon">&#xe615;</i>
                            </button>
                        </div>
                        <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
                           onclick="reload();" title="刷新">
                            <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>
                        </a>
                    </form>
                </div>
                <div class="layui-card-body ">
                    <table id="blogLinkList" lay-filter="blogLinkList"></table>
                </div>
            </div>
        </div>
    </div>
</div>
<div style="display: none" class="layui-fluid" id="updateCorn">
    <div class="layui-row">
        <div class="layui-form">
            <div class="layui-form-item">
                <label for="update_task_cron" class="layui-form-label">
                    <span class="x-red">*</span>Cron表达式</label>
                <div class="layui-input-inline">
                    <input type="text" id="update_task_cron" name="cronExpression" lay-verify="required"
                           autocomplete="off" class="layui-input"></div>
            </div>
        </div>
    </div>
</div>

<!--<script type="text/html" id="isDeletedTool">-->
<!--    {{#  if(d.isDeleted == 1){ }}-->
<!--    <input type="checkbox" name="isDeleted" lay-skin="switch" value="{{d.linkId}}"-->
<!--           lay-text="已删除|未删除" lay-filter="isDeleted" checked>-->
<!--    {{#  } else { }}-->
<!--    <input type="checkbox" name="isDeleted" lay-skin="switch" value="{{d.linkId}}"-->
<!--           lay-text="已删除|未删除" lay-filter="isDeleted">-->
<!--    {{#  } }}-->
<!--</script>-->
<script type="text/html" id="blogListBar">
    <!--    <a class="layui-btn layui-btn-xs" lay-event="handlePause">暂停</a>-->
    <!--    <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="handleResume">恢复</a>-->
    <!--    <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="handleDelete">删除</a>-->
    <!--    <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="handleUpdate">修改</a>-->
</script>
<script type="text/html" id="hint">
    <!--    <button class="layui-btn layui-btn-sm" lay-event="addJob">添加定时任务</button>-->
    <!--    <button class="layui-btn layui-btn-sm" lay-event="readCron">查看Cron表达式</button>-->
</script>
<script type="text/javascript" th:inline="none">
    layui.use(['form', 'layer', 'table', 'laydate'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.jquery,
            table = layui.table,
            laydate = layui.laydate;

        //日期
        laydate.render({
            elem: '#date'
        });

        var formSelects = layui.formSelects;

        formSelects.config("methodType", {
            type: 'get',
            keyName: 'methodType',
            keyVal: 'methodCode',
            response: {
                statusCode: 200,
                statusName: 'resultCode',
                msgName: 'message',
                dataName: 'data'
            }
        });

        formSelects.data('methodType', 'server', {
            url: '/admin/methodType/list'
        });

        /**
         *  初始化 列表
         */
        var tableIns = table.render({
            elem: '#blogLinkList',
            id: "blogLinkTable",
            url: '/admin/listWeblog',
            title: '链接列表',
            page: true,
            height: "full-110",
            toolbar: "#hint",
            limits: [10],
            cols: [[
                // {type: 'checkbox'},
                {field: 'description', title: '操作描述', align: "center", unresize: "true"},
                // {field: 'startTime', title: '操作时间(s毫秒)', align: 'center', unresize: "true"},
                {field: 'timeCost', title: '消耗时间(s毫秒)', align: 'center', unresize: "true"},
                {field: 'url', title: 'URL', align: 'center', unresize: "true"},
                {field: 'uri', title: 'URI', align: 'center', unresize: "true"},
                {field: 'httpMethod', title: '请求类型', align: 'center', unresize: "true"},
                {field: 'ipAddress', title: 'IP地址', align: 'center', unresize: "true"},
                {
                    field: 'methodType', title: '操作类型', align: 'center', unresize: "true", templet: function (d) {
                        if (d.methodType == '1') {
                            return "其他";
                        } else if (d.methodType == '2') {
                            return "新增博客";
                        } else if (d.methodType == '3') {
                            return "修改博客";
                        } else if (d.methodType == '4') {
                            return "删除博客";
                        } else if (d.methodType == '5') {
                            return "添加评论";
                        }
                    }
                },
                // {field: 'isDeleted', title: '当前状态', align: 'center', unresize: "true", templet: '#isDeletedTool'},
                // {title: '操作', unresize: "true", width: 280, toolbar: "#blogListBar"}
            ]]
        });
        //搜索
        form.on('submit(search_btn)', function (data) {
            var datas = data.field;
            for (var property in datas) {
                if (datas[property] === "") {
                    delete datas[property];
                }
            }
            tableIns.reload({
                where: data.field
                , page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
            return false;
        });

        // form.on('switch(isDeleted)', function (data) {
        //     var isDeleted = data.elem.checked ? 1 : 0; //1为已删除,0为未删除
        //     var index = top.layer.msg('数据提交中，请稍候', {icon: 16, time: false, shade: 0.8});
        //     $.post("/admin/v1/link/isDel",
        //         {
        //             "linkId": data.value,
        //             "isDeleted": isDeleted
        //         }, function (s) {
        //             setTimeout(function () {
        //                 top.layer.close(index);
        //                 form.render('select', 'isDeleted');
        //                 top.layer.msg(s.message);
        //             }, 500);
        //         });
        // });

        //头工具栏事件
        table.on('toolbar(blogLinkList)', function (obj) {
            if (obj.event === 'addJob') {
                xadmin.open('添加任务', '/admin/job/add', 450, 400);
            }
            if (obj.event == "readCron") {
                layer.open({
                    type: 1
                    , offset: "auto"
                    , id: 'layerDemo'
                    , content: $('#cronDiv')
                    , btn: '确定'
                    , btnAlign: 'c' //按钮居中
                    , shade: 0 //不显示遮罩
                    , area: ['800px', '600px']
                    , yes: function () {
                        layer.closeAll();
                    }
                });
            }
        });

        //监听工具条
        table.on('tool(blogLinkList)', function (obj) {
            var data = obj.data, //获得当前行数据
                layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            switch (layEvent) {
                // 暂停
                case 'handlePause':
                    $.post("/admin/pausejob",
                        {
                            "jobClassName": data.job_NAME,
                            "jobGroupName": data.job_GROUP
                        }, function (s) {
                            if (s.resultCode == 200) {
                                layer.msg("暂停任务成功！");
                            } else {
                                layer.msg("暂停任务失败！请重试");
                            }
                        });
                    layer.closeAll();
                    tableIns.reload();
                    break;
                // 恢复
                case 'handleResume':
                    $.post("/admin/resumejob",
                        {
                            "jobClassName": data.job_NAME,
                            "jobGroupName": data.job_GROUP
                        }, function (s) {
                            if (s.resultCode == 200) {
                                layer.msg("恢复任务成功！");
                            } else {
                                layer.msg("恢复任务失败！请重试");
                            }
                        });
                    layer.closeAll();
                    tableIns.reload();
                    break;
                // 删除
                case 'handleDelete':
                    layer.confirm('确定删除该定时任务？', {
                        btn: ['删除', '取消'] //按钮
                    }, function () {
                        $.post("/admin/deletejob",
                            {
                                "jobClassName": data.job_NAME,
                                "jobGroupName": data.job_GROUP,
                            }, function (s) {
                                if (s.resultCode == 200) {
                                    layer.msg("删除任务成功！");
                                } else {
                                    layer.msg("删除任务失败！请重试");
                                }
                                layer.closeAll();
                                tableIns.reload();
                            });
                    }, function () {
                        layer.closeAll();
                    });
                    break;
                // 修改
                case 'handleUpdate':
                    layer.open({
                        type: 1
                        , offset: "auto"
                        , id: 'layerDemo'
                        , content: $('#updateCorn')
                        , btn: '确定'
                        , btnAlign: 'c' //按钮居中
                        , shade: 0 //不显示遮罩
                        , yes: function () {
                            var cron = $('#update_task_cron').val();

                            var res = cronValidate(cron);

                            if ((!typeof res) == "boolean") {
                                layer.msg(res);
                                return;
                            }
                            $.post("/admin/reschedulejob",
                                {
                                    "jobClassName": data.job_NAME,
                                    "jobGroupName": data.job_GROUP,
                                    "cronExpression": cron
                                }, function (s) {
                                    if (s.resultCode == 200) {
                                        layer.msg("修改任务Cron表达式成功！");
                                    } else {
                                        layer.msg("修改任务Cron表达式失败！请重试");
                                    }
                                    layer.closeAll();
                                    tableIns.reload();
                                });
                        }
                    });
                    break;
            }
        });
    });

    function reload() {
        $.get("/admin/v1/reload", function (data) {
            if (data) {
                location.reload();
            } else {
                parent.location.reload();
            }
        });
    }
</script>
</body>
</html>